/**
 * Title: SelectSearchItemActivity.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-12-17 下午9:13:39
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.logic.common.SearchConfig;
import com.luoxudong.phonenumber.logic.common.value.SearchType;
import com.luoxudong.phonenumber.logic.db.model.AreaInfo;
import com.luoxudong.phonenumber.logic.search.SearchFactory;
import com.luoxudong.phonenumber.logic.search.interfaces.ISearchManager;
import com.luoxudong.phonenumber.logic.search.vo.SearchItem;
import com.luoxudong.phonenumber.ui.adapters.SelectSearchItemListAdapter;
import com.luoxudong.phonenumber.ui.common.listener.OnItemClickAvoidForceListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.ITitleBarOnClickListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.TitleBarView;

/** 
 * ClassName: SelectSearchItemActivity
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2013-12-17 下午9:13:39
 */
public class SelectSearchItemActivity extends AbsSubActivity{
	private TitleBarView titleBarView = null;
	private List<SearchItem> searchDatas = new ArrayList<SearchItem>();
	private SelectSearchItemListAdapter adapter = null;
	private ListView searchSelectItemListView = null;
	private int flag = 0;
	private ISearchManager searchManager = null;
	
	@Override
	protected int getContentViewLayoutResId() {
		return R.layout.search_select_layout;
	}

	@Override
	protected void onCreateFindView(Bundle savedInstanceState) {
		titleBarView = (TitleBarView)findViewById(R.id.title_bar);
		searchSelectItemListView = (ListView)findViewById(R.id.lv_search_item);
		titleBarView.changeMode(TitleBarView.MODEL_BACK);
	}

	@Override
	protected void onCreateAddListener(Bundle savedInstanceState) {
		titleBarView.setTitleBarOnClickListener(titleBarOnClickListener);
		searchSelectItemListView.setOnItemClickListener(onItemClickListener);
	}

	@Override
	protected void onCreateInitData(Bundle savedInstanceState) {
		flag = getIntent().getIntExtra("flag", 0);
		String[] values = getResources().getStringArray(R.array.search_item);
		titleBarView.setTitle(values[flag]);
		
		searchDatas.clear();
		adapter = new SelectSearchItemListAdapter(this, searchDatas);
		searchSelectItemListView.setAdapter(adapter);
		searchManager = SearchFactory.getSearchManager();
	}

	@Override
	protected void onCreateTaskLoadData() {
		if (flag == SearchType.area.getValue())
		{
			SearchItem searchItem = (SearchItem)getIntent().getSerializableExtra("searchItem");
			int areaId = 0;
			if (searchItem != null)
			{
				areaId = searchItem.getId();
			}
			
			searchDatas = getAreas(areaId);
		}
		else if (flag == SearchType.auspicious.getValue())
		{
			searchDatas = arrayToList(SearchConfig.auspicious);
		}
		else if (flag == SearchType.birthday.getValue())
		{
			searchDatas = arrayToList(SearchConfig.birthdays);
		}
		else if (flag == SearchType.brand.getValue())
		{
			searchDatas = arrayToList(SearchConfig.brands);
		}
		else if (flag == SearchType.endian.getValue())
		{
			searchDatas = arrayToList(SearchConfig.endians);
		}
		else if (flag == SearchType.numberSegment.getValue())
		{
			searchDatas = arrayToList(SearchConfig.numberSegments);
		}
		else if (flag == SearchType.packa.getValue())
		{
			searchDatas = arrayToList(SearchConfig.packas);
		}
		else if (flag == SearchType.price.getValue())
		{
			searchDatas = arrayToList(SearchConfig.prices);
		}
	}

	@Override
	protected void onCreateTaskAddView() {
		sendMessage(obtainMessage(MSG_REFRESH_UI, null));
	}

	@Override
	protected void refreshUI(Object obj) {
		adapter.clear();
		adapter.appendToTopList(searchDatas);
		adapter.notifyDataSetChanged();
	}
	
	private List<SearchItem> arrayToList(SearchItem[] searchItems)
	{
		List<SearchItem> searchItemList = new ArrayList<SearchItem>();
		
		if (searchItems != null)
		{
			for (SearchItem searchItem : searchItems)
			{
				searchItemList.add(searchItem);
			}
		}
		return searchItemList;
	}
	
	private List<SearchItem> getAreas(int areaId)
	{
		List<SearchItem> searchItemList = new ArrayList<SearchItem>();
		
		List<AreaInfo> areaInfos = searchManager.findAreaInfosByParentAreaId(areaId);
		
		if (areaInfos != null)
		{
			SearchItem searchItem = null;
			for (AreaInfo areaInfo : areaInfos)
			{
				int childCount = searchManager.getChildAreaCount(areaInfo.getAreaId());
				searchItem = new SearchItem();
				searchItem.setId(areaInfo.getAreaId());
				searchItem.setHaveChild(childCount > 0 ? true : false);
				searchItem.setSearchName(areaInfo.getAreaName());
				
				searchItemList.add(searchItem);
			}
		}
		return searchItemList;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		int f = getIntent().getIntExtra("flag", -1);
		if (f == SearchType.area.getValue())
		{
			SearchItem searchItem = (SearchItem)data.getSerializableExtra("searchItem");
			Intent intent = new Intent(SelectSearchItemActivity.this, SearchActivity.class);
			intent.putExtra("searchItem", searchItem);
			gobackWithResult(flag, intent);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			goback();
			return true;
			
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private ITitleBarOnClickListener titleBarOnClickListener = new ITitleBarOnClickListener() {
		
		@Override
		public void onRightClick(View v) {
			
		}
		
		@Override
		public void onLeftClick(View v) {
			goback();
		}
	};
	
	private OnItemClickAvoidForceListener onItemClickListener = new OnItemClickAvoidForceListener() {
		
		@Override
		public void onItemClickAvoidForce(AdapterView<?> parent, View view, int position, long id) {
			SearchItem searchItem = searchDatas.get(position);
			if (flag == SearchType.area.getValue() && searchItem != null && searchItem.isHaveChild())
			{
				SearchItem item = (SearchItem)getIntent().getSerializableExtra("searchItem");
				if (item != null && searchItem.getId() == -1)
				{
					searchItem = item;
				}
				else if (searchItem.getId() != -1)
				{
					Intent intent = new Intent(SelectSearchItemActivity.this, SelectSearchItemActivity.class);
					intent.putExtra("flag", flag);
					intent.putExtra("searchItem", searchItem);
					startActivityForResult(intent, position);
					return;
				}
			}
			
			Intent intent = new Intent(SelectSearchItemActivity.this, SearchActivity.class);
			intent.putExtra("searchItem", searchItem);
			intent.putExtra("flag", flag);
			gobackWithResult(position, intent);
			//finish();
		}
	};
}
